import { FC, useContext } from "react";
import { playMusicDispatchContext } from "@/reducers/playMusic";

import { getSongUrl } from "@/api/common/index";
import styles from "./index.module.scss";
interface IProps {
  list: playList[];
}
const PlayMusicList: FC<IProps> = ({ list }) => {
  const dispatch = useContext(playMusicDispatchContext);
  const playMusic = (it: playList) => {
    palySong(it);
  };

  const palySong = async (it: playList) => {
    const url = await getSongUrl(it.id);
    if (!url) {
      alert("暂无版权");
      return;
    }
    dispatch({
      type: "PLAY",
      paLoad: {
        keepOrder: false,
        musicUrl: url,
        musicId: it.id,
        picUrl: it.picUrl,
        musicName: it.name,
        song: it.songName,
        autoPlay: true,
      },
    });
  };
  return (
    <div className={styles.PlayMusicList}>
      <div className={styles.list_wrap}>
        {list.map((it, index) => {
          return (
            <div
              onClick={() => playMusic(it)}
              className={styles.listItem}
              key={index}
            >
              <div className={styles.index_wrap}>{index + 1}</div>
              <div className={styles.content_wrap}>
                <div className={styles.name}>{it.name}</div>
                <div className={styles.songName}>{it.songName}</div>
              </div>
              <div className={styles.right}>
                <svg
                  viewBox="0 0 1024 1024"
                  version="1.1"
                  xmlns="http://www.w3.org/2000/svg"
                  width="24"
                  height="24"
                >
                  <path d="M440 112m72 0l0 0q72 0 72 72l0 0q0 72-72 72l0 0q-72 0-72-72l0 0q0-72 72-72Z"></path>
                  <path d="M440 440m72 0l0 0q72 0 72 72l0 0q0 72-72 72l0 0q-72 0-72-72l0 0q0-72 72-72Z"></path>
                  <path d="M440 768m72 0l0 0q72 0 72 72l0 0q0 72-72 72l0 0q-72 0-72-72l0 0q0-72 72-72Z"></path>
                </svg>
              </div>
            </div>
          );
        })}
      </div>
    </div>
  );
};

export default PlayMusicList;
